cmake_minimum_required(VERSION 3.16)

project(yolosort)

include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/common.cmake)
message(STATUS "PROJECT_SOURCE_DIR:${PROJECT_SOURCE_DIR}")
message(STATUS "yolo TensorRT_ROOT:${TensorRT_ROOT}")

#set(CMAKE_CXX_FLAGS "-std=c++0x -Wno-deprecated-declarations")
set(CMAKE_CXX_FLAGS "-Wno-deprecated-declarations")
add_definitions(-std=c++14)
add_definitions(-DAPI_EXPORTS)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_BUILD_TYPE Release)

list(APPEND CUDA_NVCC_FLAGS "-std=c++14")
option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)

# find thirdparty
find_package(CUDA REQUIRED)
find_package(OpenCV REQUIRED)

# gather cuda lib
list(APPEND ALL_LIBS 
  ${CUDA_LIBRARIES} 
  ${CUDA_cublas_LIBRARY} 
  ${CUDA_nppc_LIBRARY} 
  ${CUDA_nppig_LIBRARY} 
  ${CUDA_nppidei_LIBRARY} 
  ${CUDA_nppial_LIBRARY})

# include cuda's header
list(APPEND INCLUDE_DRIS ${CUDA_INCLUDE_DIRS})

# gather TensorRT lib
#set(TensorRT_ROOT /workplace/detect_track/TensorRT-8.4.2.4)

find_library(TRT_NVINFER NAMES nvinfer HINTS ${TensorRT_ROOT} PATH_SUFFIXES lib lib64 lib/x64)
find_library(TRT_NVINFER_PLUGIN NAMES nvinfer_plugin HINTS ${TensorRT_ROOT} PATH_SUFFIXES lib lib64 lib/x64)
find_library(TRT_NVONNX_PARSER NAMES nvonnxparser HINTS ${TensorRT_ROOT} PATH_SUFFIXES lib lib64 lib/x64)
find_library(TRT_NVCAFFE_PARSER NAMES nvcaffe_parser HINTS ${TensorRT_ROOT} PATH_SUFFIXES lib lib64 lib/x64)
find_path(TENSORRT_INCLUDE_DIR NAMES NvInfer.h HINTS ${TensorRT_ROOT} PATH_SUFFIXES include)
list(APPEND ALL_LIBS ${TRT_NVINFER} ${TRT_NVINFER_PLUGIN} ${TRT_NVONNX_PARSER} ${TRT_NVCAFFE_PARSER})

# include tensorrt's headers
list(APPEND INCLUDE_DRIS ${TENSORRT_INCLUDE_DIR})

# include tensorrt's sample/common headers
set(SAMPLES_COMMON_DIR ${TensorRT_ROOT}/samples/common)
list(APPEND INCLUDE_DRIS ${SAMPLES_COMMON_DIR})

include_directories(
  ${INCLUDE_DRIS}
  ${OpenCV_INCLUDE_DIRS}
)

# ===== deepsort =====
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/deepsort/include)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/deepsort/src SRC_DIR)
add_library(deepsort SHARED ${SRC_DIR})
target_link_libraries(deepsort ${ALL_LIBS} ${OpenCV_LIBS} )

# ===== yolov8 =====
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/yolov8)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/yolov8/utils)

file(GLOB YOLO_CPPS 
  ${CMAKE_CURRENT_SOURCE_DIR}/yolov8/*.cpp
  ${CMAKE_CURRENT_SOURCE_DIR}/yolov8/*.cu
  ${CMAKE_CURRENT_SOURCE_DIR}/yolov8/utils/*.cu
  ${CMAKE_CURRENT_SOURCE_DIR}/yolov8/utils/*.cpp
  ${TensorRT_ROOT}/samples/common/logger.cpp 
  ${TensorRT_ROOT}/samples/common/sampleOptions.cpp
  )
list(REMOVE_ITEM YOLO_CPPS app_yolov8.cpp)
list (LENGTH CPPS length)
cuda_add_library(yolov8 SHARED ${YOLO_CPPS})
target_link_libraries(yolov8 ${ALL_LIBS} ${OpenCV_LIBRARIES})

# ===== main =====
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src M_SRC_DIR)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

add_executable(yolosort ${M_SRC_DIR})

target_link_libraries(yolosort deepsort yolov8)

if(UNIX)
add_definitions(-O2 -pthread)
endif(UNIX)
